import {
  Inject,
  Controller,
  Post,
  Get,
  Body,
  Put,
  Param,
  Del
} from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { HomeService } from "../service/home.service";
import { Utils } from "../utils/index";
import { ApiTags } from "@midwayjs/swagger";
import {
  SectionCreateDto,
  ContentCreateDto,
  BannerCreateDto
} from "../dto/home.dto";

@ApiTags("企业简介")
@Controller()
export class HomeController {
  @Inject()
  ctx: Context;

  @Inject()
  homeService: HomeService;

  @Inject()
  utils: Utils;

  @Post("/auth-api/section", { summary: "添加段落" })
  async add_home_section(@Body() dto: SectionCreateDto) {
    return this.utils.response.success({
      data: await this.homeService.add_home_section(dto)
    });
  }

  @Put("/auth-api/section/:sectionId", { summary: "修改段落" })
  async update_home_section(
    @Body() dto: SectionCreateDto,
    @Param("sectionId") sectionId: string
  ) {
    return this.utils.response.success({
      data: await this.homeService.update_home_section(dto, sectionId)
    });
  }

  @Del("/auth-api/section/:sectionId", { summary: "删除段落" })
  async remove_home_section(@Param("sectionId") sectionId: string) {
    return this.utils.response.success({
      data: await this.homeService.remove_home_section(sectionId)
    });
  }

  @Get("/common-api/section/list", { summary: "获取段落列表" })
  async get_section_list() {
    const result = await this.homeService.get_section_list();
    return this.utils.response.success(result);
  }

  @Post("/auth-api/section-content", { summary: "添加段落内容" })
  async add_section_content(@Body() dto: ContentCreateDto) {
    return this.utils.response.success({
      data: await this.homeService.add_section_content(dto)
    });
  }

  @Put("/auth-api/section-content/:contentId", { summary: "修改段落内容" })
  async update_section_content(
    @Body() dto: ContentCreateDto,
    @Param("contentId") contentId: string
  ) {
    return this.utils.response.success({
      data: await this.homeService.update_section_content(dto, contentId)
    });
  }

  @Del("/auth-api/section-content/:contentId", { summary: "删除段落内容" })
  async remove_section_content(@Param("contentId") contentId: string) {
    return this.utils.response.success({
      data: await this.homeService.remove_section_content(contentId)
    });
  }

  @Get("/auth-api/section-content/list/:sectionId", {
    summary: "获取段落内容列表"
  })
  async get_section_content_list(@Param("sectionId") sectionId: string) {
    const result = await this.homeService.get_section_content_list(sectionId);
    return this.utils.response.success(result);
  }

  @Post("/auth-api/banner", { summary: "添加轮播图" })
  async add_home_banner(@Body() dto: BannerCreateDto) {
    return this.utils.response.success({
      data: await this.homeService.add_home_banner(dto)
    });
  }

  @Put("/auth-api/banner/:bannerId", { summary: "添加轮播图" })
  async update_home_banner(
    @Body() dto: BannerCreateDto,
    @Param("bannerId") bannerId: string
  ) {
    return this.utils.response.success({
      data: await this.homeService.update_home_banner(dto, bannerId)
    });
  }

  @Del("/auth-api/banner/:bannerId", { summary: "添加轮播图" })
  async remove_home_banner(@Param("bannerId") bannerId: string) {
    return this.utils.response.success({
      data: await this.homeService.remove_home_banner(bannerId)
    });
  }

  @Get("/auth-api/banner/list", { summary: "获取轮播图列表" })
  async get_banner_list() {
    const result = await this.homeService.get_banner_list();
    return this.utils.response.success({
      ...result
    });
  }

  /**
   * 获取首页信息，包括轮播图、段落、段落内容
   */
  @Get("/common-api/home", { summary: "获取首页信息" })
  async get_home_info() {
    const result = await this.homeService.get_home_info();
    return this.utils.response.success({ ...result });
  }
}
